package establishmentplanner;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

 

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.GregorianCalendar;

/**
 *
 * @author James
 */
public class Parser {

    private String fileName;
    private ArrayList<FlightData> flightDataList;

    public Parser(String fileName){
        this.fileName = fileName;
        this.flightDataList = new ArrayList<FlightData>();
    }

    /**
     * Returns an ArrayList<FlightData> with each of the FlightData object's fields containing sanitised strings.
     * @return ArrayList<FlightData>
     * @throws FileNotFoundException
     * @throws IOException
     */
    public ArrayList<FlightData> readFromFile() throws FileNotFoundException, IOException{
        FileReader fr = new FileReader(fileName);
        BufferedReader reader = new BufferedReader(fr);
        String line;
        String[] split;
        reader.readLine(); //skip 1st line
        while((line = reader.readLine()) != null){ //read line by line untill end of file
            split = line.split(","); // split each line into discrete fields with "," delimiter
            split = sanitizeStrings(split); //strips leading and trailing "'s
            FlightData fd = new FlightData(
                    getDate(split[0]),
                    split[1],
                    split[2],
                    split[3],
                    split[4],
                    split[5],
                    split[6],
                    split[7],
                    split[8],
                    split[9],
                    split[10],
                    split[11] );
            flightDataList.add(fd);
            reader.readLine();//skip next line
        }

        return flightDataList;
    }

    /**
     * Returns a GregorianCalender date object
     * @param date
     * @return
     */
    private GregorianCalendar getDate(String date){        
        int year = Integer.parseInt(date.substring(5, 7));
        int month = getMonthInt(date.substring(2,5));
        int day = Integer.parseInt(date.substring(0, 1));
        GregorianCalendar gc = new GregorianCalendar(year,month,day);
        return gc;
    }

    /**
     * Utility method to return a month integer from a 3 letter month code.
     * NOT case sensitive. JAN = 0 ... Dec = 11
     * @param month
     * @return
     */
    private int getMonthInt(String month){
        String [] months = {"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};
        int i = 0;
        while(!month.equalsIgnoreCase(months[i])){
            i++;
        }
        return i;
    }

    /**
     * Chops off the leading and trailing characters of the string.
     * Required to stip 2's from the string.
     * NOTE: Very specific to the CSV file, may look into a more generalised approach.
     * @param strings
     * @return
     */
    private String[] sanitizeStrings(String[] strings){
        String [] sanitizedStrings = new String[strings.length];
        for(int i = 0; i < strings.length; i++){
            sanitizedStrings[i] = strings[i].substring(1, strings[i].length()-1);
        }
        return sanitizedStrings;
    }
}